/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 12.09.12
 * Time: 10:15
 * To change this template use File | Settings | File Templates.
 */
package fgui.content {
import fgui.GuiComponent;

import fgui.UIGraphicsComponent;

import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.geom.Point;

import flash.net.URLRequest;

import mx.controls.Image;

public class ImageContainer extends GuiComponent {
    private var loader:Loader = new Loader();
    private var source:String;
    private var maskOfComponent:UIGraphicsComponent = new UIGraphicsComponent();

    public function ImageContainer(coordinate:Point, size:Point, src:String) {
        super(coordinate, size);
        this.source = src;

        this.x = coordinate.x;
        this.y = coordinate.y;

        loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady);
        loader.load(new URLRequest(src));

        maskOfComponent.drawRoundedRectangle(0x000000, 1, new Point(), size, new Point(20, 20));
        addChild(maskOfComponent);
        loader.mask = maskOfComponent;

        this.drawBorderRectangle(1, 0x000000, 1, new Point(-margin, -margin), new Point(size.x + margin, size.y + margin));
    }

    public function onProgressStatus(e:ProgressEvent):void {
        trace("[INFO] - Load image - '" + this.source + "'; Size (loaded;total) - " + "(" + e.bytesLoaded + ";" + e.bytesTotal + ")");
    }

    public function onLoaderReady(e:Event):void {
        var change:Number = 0;
        var changeWidth:Number = loader.width / this.getSize().x;
        var changeHeight:Number = loader.height / this.getSize().y;

        var positionImage:Point;

        if (changeWidth >= changeHeight) {
            change = changeWidth;
            loader.content.width /= change;
            loader.content.height /= change;
            positionImage = new Point(
                    0,
                    this.getSize().y / 2 - loader.content.height / 2
                    );
        }
        else {
            change = changeHeight;
            loader.content.width /= change;
            loader.content.height /= change;
            positionImage = new Point(
                    this.getSize().x / 2 - loader.content.width / 2,
                    0
                    );
        }

        loader.x = positionImage.x;
        loader.y = positionImage.y;

        maskOfComponent.x = positionImage.x;
        maskOfComponent.y = positionImage.y;
        maskOfComponent.drawRoundedRectangle(
                0x000000,
                1,
                new Point(),
                new Point(
                        loader.content.width,
                        loader.content.height
                        ),
                new Point(20, 20)
                );

        addChild(loader);
    }
}
}
